package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author zsk
 * @since 2022-04-11
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    /**
     * 新增作者信息
     * @param author
     * @return
     */
    @Override
    public ResponseResult<ApAuthor> saveAuthor(ApAuthor author) {

        //判断传递的作者信息是否为空
        if (author == null || author.getUserId() == null){
            //返回结果
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //构建查询条件，是否已经存储该用户
        LambdaQueryWrapper<ApAuthor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApAuthor::getUserId,author.getUserId());

        ApAuthor one = this.getOne(wrapper);
        //判断
        if (one != null){
            //证明是老用户
            return ResponseResult.okResult(one);
        }
        //新用户保存
        this.save(author);

        //返回结果
        return ResponseResult.okResult(author);
    }
}
